clear all
set more off
set maxvar 10000

        
	use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
    keep if baseline_sample==1 
        
    * Decades (grouped)
	gen early = (decade>=1910 & decade<=1920)
    gen late = (decade>=1940 & decade<=1950)
    keep if early==1 | late==1
    
    * Sex x marital status groups for black respondents
    gen group=1 if race==2 & sex==1
    replace group=2 if race==2 & sex==2 &  married==1
    replace group=3 if race==2 & sex==2 &  married==0
    tab group, m
    drop if group==.
    
    bysort late: tab group if sex==2 [aw=wgt_sex_race]
	
	* Residualize
	
	//logs
	gen yhat_fam= .
	gen yhat_father= .
	
	foreach name in early late { 
	forval r=1/3 { 
	
		quietly reg log_son_baseline i.decade if `name'==1 & group==`r'  [aw=wgt_sex_race]
					predict yhat_fam`name'`r' if `name'==1 & group==`r' , residuals
					sum log_son_baseline if `name'==1 & group==`r'  [aw=wgt_sex_race]
					replace yhat_fam = yhat_fam`name'`r' + `r(mean)' if `name'==1 & group==`r' 
			
		quietly reg log_father_baseline i.decade if `name'==1 & group==`r' [aw=wgt_sex_race]
					predict yhat_father`name'`r' if `name'==1 & group==`r' , residuals
					sum log_father_baseline if `name'==1 & group==`r' [aw=wgt_sex_race]
					replace yhat_father = yhat_father`name'`r' + `r(mean)' if `name'==1 & group==`r' 
	}
	}
	
	replace log_son_baseline = yhat_fam 
	replace log_father_baseline = yhat_father
	drop yhat_fam* yhat_father*
	
	//ranks
	gen yhat_fam= .
	gen yhat_father= .
	
	foreach name in early late { 
	forval r=1/3 { 
	
		quietly reg rank_son_baseline i.decade if `name'==1 & group==`r'  [aw=wgt_sex_race]
					predict yhat_fam`name'`r' if `name'==1 & group==`r' , residuals
					sum rank_son_baseline if `name'==1 & group==`r'  [aw=wgt_sex_race]
					replace yhat_fam = yhat_fam`name'`r' + `r(mean)' if `name'==1 & group==`r' 
			
		quietly reg rank_father_baseline i.decade if `name'==1 & group==`r' [aw=wgt_sex_race]
					predict yhat_father`name'`r' if `name'==1 & group==`r' , residuals
					sum rank_father_baseline if `name'==1 & group==`r' [aw=wgt_sex_race]
					replace yhat_father = yhat_father`name'`r' + `r(mean)' if `name'==1 & group==`r' 
	}
	}
	
	replace rank_son_baseline = yhat_fam 
	replace rank_father_baseline = yhat_father
	drop yhat_fam* yhat_father*
	
                
    tempfile fulldata
    save `fulldata'
        
    * Binscatters by time period
        foreach period in 0 1  {

            use `fulldata', clear
            keep if late==`period'
            
            foreach g in 1 2 3   {    
            //1. Ranks
                binscatter rank_son_baseline rank_father_baseline if group==`g' [aw=wgt_sex_race], nq(10) savedata("binscatter_rank_group`g'") replace       
            //2. IGE
                binscatter log_son_baseline log_father_baseline if group==`g' [aw=wgt_sex_race], nq(10) savedata("binscatter_ige_group`g'") replace
            }
            
        * Save binscatters
            foreach g in 1 2 3   {
            foreach measure in rank ige {
                
                    insheet using "binscatter_`measure'_group`g'.csv", clear 
                    gen group = `g'
                    gen measure ="`measure'"
                    save "binscatter_`measure'_group`g'.dta", replace 
                }       
            }   

        * Append
            use binscatter_rank_group1, clear
            append using binscatter_ige_group1
            foreach g in 2 3  {
                append using binscatter_ige_group`g'
                append using binscatter_rank_group`g'
            }
                
                
        * Run regressions on the full panel; save constant and slope 
            gen slope=.
            gen intercept=.

            foreach measure in rank ige  {
                foreach g in 1 2 3   {
            
                    preserve
                    use `fulldata', clear
                    keep if late==`period'
                    keep if group==`g'
                    
                    if "`measure'"=="ige" {     
            
                        reg log_son_baseline log_father_baseline [aw=wgt_sex_race]
                        restore
                        replace slope = _b[log_father_baseline] if measure=="ige" & group==`g'
                        replace intercept = _b[_cons] if measure=="ige" & group==`g'
                        
                    }
                        
                    if "`measure'"=="rank"  {
                    
                        reg rank_son_baseline rank_father_baseline [aw=wgt_sex_race]
                        restore         
                        replace slope = _b[rank_father_baseline] if measure=="rank" & group==`g'
                        replace intercept = _b[_cons] if measure=="rank" & group==`g'
                        
                    }                                                   
                }
            }              
            
        * Generate predicted values for each point using slope and intercept
        gen yhat_line = intercept + (slope*rank_father_baseline) if measure=="rank"
        replace yhat_line = intercept + (slope*log_father_baseline) if measure=="ige"

        * Figures
        if "`period'"=="0" local title "early"
        if "`period'"=="1" local title "late"

        * IGE
        #delimit ;
            twoway (scatter log_son_baseline log_father_baseline if group==1 & measure=="ige", m(circle_hollow) mc(red)) 
                   (line yhat_line log_father_baseline if group==1 & measure=="ige", lp(dash) lc(red) lwidth(0.25))  
                   (scatter log_son_baseline log_father_baseline if group==2 & measure=="ige", m(triangle_hollow) mc(orange*0.7)) 
                   (line yhat_line log_father_baseline if group==2 & measure=="ige", lp(dash) lc(orange*0.7) lwidth(0.25))
                   (scatter log_son_baseline log_father_baseline if group==3 & measure=="ige", m(diamond_hollow) mc(pink*0.7)) 
                   (line yhat_line log_father_baseline if group==3 & measure=="ige", lp(dash) lc(pink*0.7) lwidth(0.25))  
                  ,        
        legend(on rows(4) order(1 "Black men" 3 "Black married women" 5 "Black unmarried women") ring(0) pos(5))  
        ylabel(6.5(0.5)9, axis(1)) yscale(r(6.5 9.1)) xscale(r(6.5 9.5)) xlabel(6.5(1)9.5)
        xti(" " "Predicted parental logged income") yti("Respondent logged income" " ")  ; 
        #delimit cr
        graph export "$Mydirectory2/appendix_a/IGE_black_married_`title'.pdf", as(pdf) replace 

        * Rank-rank      
        #delimit ;
            twoway (scatter rank_son_baseline rank_father_baseline if group==1 & measure=="rank", m(circle_hollow) mc(red)) 
                   (line yhat_line rank_father_baseline if group==1 & measure=="rank", lp(dash) lc(red) lwidth(0.25))  
                   (scatter rank_son_baseline rank_father_baseline if group==2 & measure=="rank", m(triangle_hollow) mc(orange*0.7)) 
                   (line yhat_line rank_father_baseline if group==2 & measure=="rank", lp(dash) lc(orange*0.7) lwidth(0.25))
                   (scatter rank_son_baseline rank_father_baseline if group==3 & measure=="rank", m(diamond_hollow) mc(pink*0.7)) 
                   (line yhat_line rank_father_baseline if group==3 & measure=="rank", lp(dash) lc(pink*0.7) lwidth(0.25))  
                   , 
        legend(on rows(4) order(1 "Black men" 3 "Black married women" 5 "Black unmarried women") ring(0) pos(5))  
         ylabel(10(20)70, axis(1)) yscale(r(15 70)) xscale(r(0 100)) xlabel(0(20)100) 
        xti(" " "Predicted parental income rank") yti("Respondent income rank" " ")  ;
        #delimit cr
        graph export "$Mydirectory2/appendix_a/Rank_black_married_`title'.pdf", replace 

    * Get rid of all binscatter-related files
        foreach g in 1 2 3   {
            foreach measure in rank ige {
            cap rm "binscatter_`measure'_group`g'.csv" 
            cap rm "binscatter_`measure'_group`g'.do" 
            cap rm "binscatter_`measure'_group`g'.dta"           
            } 
        }
    
    }
